package com.yunhoo.zh.bean;

import com.yunhoo.sys.util.common.NavigationTool;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.zh.db.impl.RegisterImpl;
import com.yunhoo.zh.model.User;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

@ManagedBean
@SessionScoped
public class RegisterBean implements Serializable {

    @Size(min = 2, max = 12, message = "长度必须在2和12之间")
    private String userName;//用户名
    @Size(min = 5, max = 15, message = "长度必须在5和15之间")
    private String pwd;//密码
    @Size(min = 5, max = 15, message = "长度必须在5和15之间")
    private String pwd_c;//确认密码    
    @Pattern(regexp = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[a-zA-Z]{2,4}$", message = "邮箱不合规")
    private String email;//邮箱
    @AssertTrue(message = "用户协议")
    private boolean agreed = false;

    @AssertTrue(message = "密码不一致")
    public boolean isPasswordsEquals() {
        return pwd.equals(pwd_c);
    }

    @AssertTrue(message = "名称已存在")
    public boolean isUserNameNotExists() {
        int userNameExists = RegisterImpl.isUserNameExists(userName);

        /* 用户名不存在 */
        if (userNameExists == 0) {
            return true;
        }

        /* 数据库异常，检测失败 */
        if (userNameExists == -1) {
            ToolKit.msgs_err("检测失败，请稍后重试");
        }

        return false;
    }
    
    @AssertTrue(message = "邮箱已被注册")
    public boolean isEmailNotExists() {
        int emailExists = RegisterImpl.isEmailExists(email);

        /* 用户名不存在 */
        if (emailExists == 0) {
            return true;
        }

        /* 数据库异常，检测失败 */
        if (emailExists == -1) {
            ToolKit.msgs_err("检测失败，请稍后重试");
        }

        return false;
    }

    /**
     * 用户注册
     */
    public String register() {

        User user = new User();
        user.setUserName(userName);
        user.setPwd(pwd);
        user.setEmail(email);
        int flag = RegisterImpl.insert(user);

        if (flag == 1) {
            ToolKit.msgs_info("已成功注册");
            return "login_zh";
        } else {
            ToolKit.msgs_err("操作失败，请稍后重试");
            return "register";
        }
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPwd_c() {
        return pwd_c;
    }

    public void setPwd_c(String pwd_c) {
        this.pwd_c = pwd_c;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isAgreed() {
        return agreed;
    }

    public void setAgreed(boolean agreed) {
        this.agreed = agreed;
    }
}
